//
//  HerosNextStoryTableViewController.m
//  LOLBOX
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 张明. All rights reserved.
//

#import "HerosNextStoryTableViewController.h"
#import "HerosPifuModelHelper.h"
#import "HerosDetailsModel.h"
#import "HerosModelHelper.h"
#import "HerosContentTableViewCell.h"
#import "HerosStroyTableViewCell.h"
#import "HerosNextFatherViewController.h"
#import "UIImageView+WebCache.h"
static CGFloat kImageHeight = 150.f;
@interface HerosNextStoryTableViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *nameLable;
@property(nonatomic,strong)UILabel *typeLable;
@property(nonatomic,strong)UILabel *priceLable;

@property(nonatomic,strong)NSMutableArray *allLikeArray;
@property(nonatomic,strong)NSMutableArray *allHateArray;

@property(nonatomic,strong)NSString *content;
@property(nonatomic,assign)CGFloat height;
@property(nonatomic,strong)NSString *price;
@end

@implementation HerosNextStoryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.tableView.frame = CGRectMake(0, 40, self.tableView.frame.size.width, self.tableView.frame.size.height *0.85);
    self.tableView.backgroundColor = [UIColor whiteColor];
    //注册cell 3
    [self.tableView registerClass:[HerosContentTableViewCell class] forCellReuseIdentifier:@"threeCell"];
     [self.tableView registerClass:[HerosStroyTableViewCell class] forCellReuseIdentifier:@"threeCellTwo"];
    //添加皮肤imageView
    HerosPifuModelHelper *helper = [HerosPifuModelHelper defaultHerosPifuHelper];
    self.imageView = [[UIImageView alloc] init];
    self.imageView.frame = CGRectMake(0, -kImageHeight , self.tableView.frame.size.width, kImageHeight);
    [self createImageViewChild];
    self.tableView.contentInset = UIEdgeInsetsMake(kImageHeight, 0, 0, 0);//设置tableView里的内容距离top的距离
    [self.view addSubview:self.imageView];
    if (helper.nameArray.count >=3) {
        self.nameLable.text = helper.nameArray[2];
    }else{
        self.nameLable.text = helper.nameArray[0];
    }
    
    if (helper.imageArray.count >=3) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:helper.imageArray[2]]];
    }else{
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:helper.imageArray[0]]];
    }
    self.price = helper.price;
    NSArray *array1 = [helper.price componentsSeparatedByString:@","];
    self.priceLable.text = [NSString stringWithFormat:@"金币:%@ 点卷:%@",array1[0],array1[1]];
    self.typeLable.text = helper.type;
    self.allLikeArray = [NSMutableArray array];
    self.allHateArray = [NSMutableArray array];
    HerosModelHelper *arrayHelper = [HerosModelHelper defaultHerosModelHelper];
    [self.allLikeArray addObjectsFromArray:arrayHelper.model.likeArray];
    [self.allHateArray addObjectsFromArray:arrayHelper.model.hateArray];
    self.content = arrayHelper.model.story;
}
#pragma mark - 往皮肤上添加控件
-(void)createImageViewChild{
    self.nameLable = [[UILabel alloc] init];
    self.nameLable.frame = CGRectMake(10, self.imageView.frame.size.height -80, self.view.frame.size.width-20, 20);
    // self.nameLable.backgroundColor = [UIColor redColor];
    self.nameLable.font = [UIFont systemFontOfSize:14];
    self.nameLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.nameLable];
    
    self.typeLable = [[UILabel alloc] init];
    self.typeLable.frame = CGRectMake(10, self.imageView.frame.size.height -60, self.view.frame.size.width - 20, 20);
    // self.typeLable.backgroundColor = [UIColor blueColor];
    self.typeLable.font = [UIFont systemFontOfSize:14];
    self.typeLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.typeLable];
    
    self.priceLable = [[UILabel alloc] init];
    self.priceLable.frame = CGRectMake(10, self.imageView.frame.size.height -40, self.view.frame.size.width - 20, 20);
    //self.priceLable.backgroundColor = [UIColor greenColor];
    self.priceLable.font = [UIFont systemFontOfSize:14];
    self.priceLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.priceLable];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.allLikeArray.count;
    }
    if (section == 1) {
        return self.allHateArray.count;
    }
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HerosStroyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeCellTwo" forIndexPath:indexPath];
        cell.contentLable.text = self.allLikeArray[indexPath.row][@"des"];
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg",self.allLikeArray[indexPath.row][@"partner"]]]];
        self.height = [cell getHeightWithString:self.allLikeArray[indexPath.row][@"des"]];
        self.height += 5;
        return cell;
    }else if (indexPath.section == 1){
        HerosStroyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeCellTwo" forIndexPath:indexPath];
        cell.contentLable.text = self.allHateArray[indexPath.row][@"des"];
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg",self.allHateArray[indexPath.row][@"partner"]]]];
        self.height = [cell getHeightWithString:self.allHateArray[indexPath.row][@"des"]];
        self.height += 5;
        return cell;
    }else{
        HerosContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeCell" forIndexPath:indexPath];
        
        cell.contentLable.text = self.content;
        self.height = [cell getHeightWithString:self.content];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.userInteractionEnabled = NO;
        return cell;
    }
    return nil;
}
//点击英雄跳转页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HerosNextFatherViewController *fatherVC = [[HerosNextFatherViewController alloc]init];
    if (indexPath.section == 0) {
        fatherVC.name = self.allLikeArray[indexPath.row][@"partner"];
        fatherVC.price = self.price;
        NSLog(@"跳转英雄价格--- %@",self.price);
    }
    if (indexPath.section == 1) {
        fatherVC.name = self.allHateArray[indexPath.row][@"partner"];
    }
    [self.navigationController pushViewController:fatherVC animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1){
        if (self.height < 50) {
            return 50;
        }else {
            return self.height;
        }
    }
    return self.height+30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor orangeColor];
    if (section == 0) {
        label.text = @"  最佳组合";
        return label;
    }else if (section == 1){
        label.text = @"  最强对手";
        return label;
    }else{
        label.text = @"  背景故事";
        return label;
    }
}
//添加下拉手势操作
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //contentOffset（类型为CGPoint，代表坐标）：是scrollview当前显示区域顶点相对于frame顶点的偏移量，比如上个例子你拉到最下面，contentoffset就是(0 ,480)，也就是y偏移了480
    CGFloat yOffset = scrollView.contentOffset.y;
    if (yOffset < -kImageHeight) {
        CGRect f = self.imageView.frame;
        f.origin.y = yOffset;
        f.size.height =  -yOffset;
        self.imageView.frame = f;
    }
    //设置image上的label的位置随着皮肤尺寸变化而变化
    self.nameLable.frame = CGRectMake(10, self.imageView.frame.size.height -80, self.view.frame.size.width-20, 20);
    self.typeLable.frame = CGRectMake(10, self.imageView.frame.size.height -60, self.view.frame.size.width - 20, 20);
    self.priceLable.frame = CGRectMake(10, self.imageView.frame.size.height -40, self.view.frame.size.width - 20, 20);

}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
